package com.boc.langxiang.ui.activity.common;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.common.RegistContract;
import com.boc.langxiang.entity.events.LoginSuccessEvent;
import com.boc.langxiang.entity.request.RegistRequest;
import com.boc.langxiang.entity.response.SmsCodeEntity;
import com.boc.langxiang.presenter.common.RegistPresenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.ui.activity.MainActivity;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.T;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegistAuthInfoActivity extends BaseToolBarActivity implements Handler.Callback, RegistContract.View {

    @BindView(R.id.nickname)
    EditText mNickname;
    @BindView(R.id.password)
    EditText mPassword;
    @BindView(R.id.send)
    TextView mSend;
    @BindView(R.id.checkcode)
    EditText mCheckcode;
    @BindView(R.id.bind)
    TextView mBind;
    @BindView(R.id.checkbox)
    CheckBox mCheckbox;
    @BindView(R.id.checkbox_gp)
    RelativeLayout mCheckboxGp;
    @BindView(R.id.deal)
    TextView mDeal;

    private RegistContract.Presenter mPresenter;
    private Handler mHandler;
    private int sec;
    private boolean mGetCheckFail;
    private String mPhone;
    private String mLx;
    private String mOpenid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist_auth_info);
        ButterKnife.bind(this);
        setToolBarTitle("填寫用戶信息");

        Intent intent = getIntent();
        mPhone = intent.getStringExtra("phone");
        mLx = intent.getStringExtra("lx");
        mOpenid = intent.getStringExtra("openid");

        mPresenter = new RegistPresenter(this);
        mHandler = new Handler(this);
    }

    @Override
    public boolean isDestroyed() {
        return true;
    }

    @OnClick({R.id.send, R.id.bind, R.id.checkbox_gp, R.id.deal})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send:
                sec = 59;
                mGetCheckFail = false;
                mSend.setClickable(false);
                mSend.setText(sec + "s");
                mHandler.sendEmptyMessageDelayed(100, 1000);
                mPresenter.sendCode(mPhone);
                break;
            case R.id.bind:
                bind();
                break;
            case R.id.checkbox_gp:
                mCheckbox.setChecked(!mCheckbox.isChecked());
                break;
            case R.id.deal:
                startActivity(new Intent(mContext, RegistXyActivity.class));
                break;
        }
    }

    private void bind() {
        String nickname = mNickname.getText().toString();
        if (StringUtil.isEmpty(nickname)) {
            T.showShort(mContext, "请输入昵称");
            return;
        }
        String password = mPassword.getText().toString();
        if (StringUtil.isEmpty(password)) {
            T.showShort(mContext, "请输入密码");
            return;
        }
        String code = mCheckcode.getText().toString();
        if (StringUtil.isEmpty(code)) {
            T.showShort(mContext, "请输入验证码");
            return;
        }
        RegistRequest request = new RegistRequest();
        request.phone = mPhone;
        request.nickname = nickname;
        request.pwd = password;
        request.smscode = code;
        request.lx = mLx;
        request.openid = mOpenid;
        request.status = mCheckbox.isChecked() ? RegistRequest.ADM : RegistRequest.COM;
        mPresenter.regist(request);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 100:
                sec--;
                if (mGetCheckFail || sec == 0) {
                    mSend.setText("发送验证码");
                    mSend.setClickable(true);
                    return true;
                }
                mSend.setText(sec + "s");
                mHandler.sendEmptyMessageDelayed(100, 1000);
                return true;
        }
        return false;
    }

    @Override
    public void onGetSmsCodeSuccess(SmsCodeEntity smsCodeEntity) {
        T.showShort(mContext, StringUtil.getValue(smsCodeEntity.getMsg()));
    }

    @Override
    public void registeSuccess() {
        T.showShort(mContext, "绑定成功");
        startActivity(new Intent(mContext, MainActivity.class));
        EventBus.getDefault().post(new LoginSuccessEvent());
    }

    @Override
    public void showLoading() {
        showProgressDialog("正在为您绑定账号...");
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        T.showShort(mContext, msg);
    }
}
